<?php

namespace App\Admin\Controllers\Sys;

use App\Models\Sys\Behivor;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class BehaviorController extends Controller
{
    use ModelForm;
    public $header = '行为列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Behivor::class, function (Grid $grid) {

            $grid->model()->orderBy('id','desc');
            $grid->id('编号')->sortable();

            $grid->name('标签名称');
            $grid->icon('图像')->display(function ($v){
                return get_img($v);
            })->image(50,50);

            $grid->score('分值')->display(function ($v){
                return $this->type == 1 ? '+' . $v : '-' . $v;
            });

            $grid->type('类型')->display(function ($v){
                return $v == 1 ? '积极的' : '消极的';
            });

            $grid->tag_id('行为标签')->using([
                '2' => '品德',
                '3' => '学习',
                '4' => '文体',
                '5' => '劳卫',
                '6' => '实践',
            ]);

            $states = [
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 9, 'text' => '否', 'color' => 'danger'],
            ];

            $grid->status('是否启用')->switch($states);

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->equal('type', '类型')
                    ->select([
                        '1'=>'积极的',
                        '2'=>'消极的'
                    ]);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Behivor::class, function (Form $form) {
            $form->text('name', '行为名称');
            $states = [
                'on'  => ['value' => 1, 'text' => '积极的', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '消极的', 'color' => 'danger'],
            ];
            $form->switch('status','类型')->states($states);
            $form->number('score', '分值')->rules('required|min:1')->min(1);
            $form->radio('tag_id', '行为标签')->options([
                '2' => '品德',
                '3' => '学习',
                '4' => '文体',
                '5' => '劳卫',
                '6' => '实践',
            ]);

            /*$icon = '';
            $form->radio('icon', '行为头像')->options(function ($v) use (&$icon){
                $icon = $v;
                return Behivor::options();
            })->checked([$icon]);*/

            $form->radio('icon', '行为头像')->options(Behivor::options())->setView('admin.form.radio');


            $form->disableViewCheck();
            $form->tools(function (Form\Tools $tools){
                $tools->disableView();
            });
        });
    }

}
